using System;
using System.Collections.Generic;

namespace UnitTesting.Fixturing
{
	class SimpleTypeDeterministric : ISimpleTypeGenerator
	{
		internal int InclusiveMin { get; private set; }

		internal int ExclusiveMax { get; private set; }

		readonly Dictionary<Type, Random> _generators = new Dictionary<Type, Random>();

		public int Seed { get; private set; }

		internal SimpleTypeDeterministric(int seed)
			: this(seed, SimpleValueProvider.INCLUSIVE_MIN, SimpleValueProvider.EXCLUSIVE_MAX)
		{
		}

		internal SimpleTypeDeterministric(int seed, int inclusiveMin, int exclusiveMax)
		{
			Seed = seed;
			InclusiveMin = inclusiveMin;
			ExclusiveMax = exclusiveMax;
			SimpleValueProvider.EnsureIsValidRange(InclusiveMin, ExclusiveMax);
		}

		public int Next(Type simpleType)
		{
			Ensure.IsNotNull(() => simpleType);
			CreateRandom(simpleType);
			return _generators[simpleType].Next(InclusiveMin, ExclusiveMax);
		}

		void CreateRandom(Type type)
		{
			if (!_generators.ContainsKey(type))
			{
				_generators.Add(type, new Random(Seed));
			}
		}
	}
}